package mappers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.management.Query;

import domains.Teacher;

import managers.DatabaseConnection;

public class TeacherMapper {
	
	public void addTeacher(Teacher t) {
		String sql = "insert into PPKEA.teacher (TeacherName, SubjectName) values (?, ?)";
		try {
			DatabaseConnection dbConnect = new DatabaseConnection();
			PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
			
			query.setString(1, t.getTeacherName());
			query.setString(2, t.getSubjectName());
			query.executeUpdate();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<String> selectTeacherName() {
		ArrayList<String> teacherNames = new ArrayList<String>();
		String sql = "SELECT TeacherName FROM PPKEA.teacher ORDER BY TeacherName";
		try {
			DatabaseConnection dbConnect = new DatabaseConnection();
			PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
			
			ResultSet rs = query.executeQuery(sql);
			
			while(rs.next()) {
				String teacherName = rs.getString("TeacherName");
				
				teacherNames.add(teacherName);
			}
			
			rs.close();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return teacherNames;
	}
	
	public ArrayList<Teacher> selectAllTeachers() {
		ArrayList<Teacher> teacherInfo = new ArrayList<Teacher>();
		String sql = "SELECT * FROM PPKEA.teacher ORDER BY TeacherID";
		try {
			DatabaseConnection dbConnect = new DatabaseConnection();
			PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
			
			ResultSet rs = query.executeQuery(sql);
			
			while(rs.next()) {
				Teacher t = new Teacher(rs.getInt("TeacherID"), rs.getString("TeacherName"), 
						rs.getString("SubjectName"));
				
				teacherInfo.add(t);
			}
			
			rs.close();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return teacherInfo;
	
	}
}
